package com.baozhun.mall.common.widget.customview.filter

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.baozhun.mall.common.R
import com.baozhun.mall.common.model.bean.FilterItem
import com.baozhun.mall.common.model.bean.FilterOptionItem
import com.baozhun.mall.common.model.bean.SizeInfo
import com.baozhun.mall.common.util.Constants
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagFlowLayout
import com.zhy.view.flowlayout.TagView
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.HashSet

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2023/2/11
 * @Version：1.0.0
 */
class FilterItemView: RelativeLayout,TagFlowLayout.OnTagClickListener, TagFlowLayout.OnSelectListener {
    var rootView : RelativeLayout? = null
    var titleView: TextView? = null
    var flowLayout: TagFlowLayout? = null
    var adapter: GenderFilterAdapter? = null
    var item: FilterItem? = null
    var selectedList = arrayListOf<String>()

    constructor(context: Context?) : super(context!!) {
        initView(context)
    }
    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
        initView(context)
    }
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context!!,
        attrs,
        defStyle
    ) {
        initView(context)
    }

    private fun initView(context: Context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.widget_filter_item_view, this) as RelativeLayout
        titleView = rootView?.findViewById(R.id.title_filter)
        flowLayout = rootView?.findViewById(R.id.filter_tag_flow_layout)
    }

    fun setData(item: FilterItem, filterParams: Map<String, String>) {
        this.item = item
        val selectedList = filterParams[item.query_param_name]
        adapter = GenderFilterAdapter(item.options, context)
        if(selectedList?.isNotEmpty() == true) {
            Log.e("TAG", item.query_param_name + "===" + selectedList)
            adapter?.setSelectedList(getSelectedList(selectedList, item.options))
        }
        titleView?.text = item.title
        flowLayout?.adapter = adapter
        flowLayout?.setOnTagClickListener(this)
        flowLayout?.setOnSelectListener(this)
    }

    fun getSelectedList(selectedListString: String, options: MutableList<FilterOptionItem>): Set<Int> {
        val set = HashSet<Int>()
        val selectedArray = selectedListString.split(",")
        options.forEachIndexed { index, option ->
            if (selectedArray.contains(option.value)) {
                set.add(index)
                selectedList.add(option.value)
            }
        }
        return set
    }

    override fun onTagClick(view: View?, position: Int, parent: FlowLayout?): Boolean {
        val tagView = view as TagView
        if (tagView.isSelected || selectedList.contains(this.item?.options?.get(position)?.value)) {
            tagView.isSelected = false
            this.item?.options?.get(position)?.let { selectedList.remove(it.value) }
        } else {
            if (tagView.tagView?.tag is SizeInfo) {
                val sizeInfo = tagView.tagView?.tag as SizeInfo
                if (!sizeInfo.soldOut) {
                    tagView.isSelected = true
                }
            } else {
                tagView.isSelected = true
                this.item?.options?.get(position)?.let { selectedList.add(it.value) }
            }
        }
        return true
    }

    fun getResult(): Pair<String, ArrayList<String>> {
        item?.query_param_name?.let {
            if (selectedList.isEmpty()) {
                return Pair("", arrayListOf())
            }
            return Pair(it, selectedList)
        }
        return Pair("", arrayListOf())
    }

    fun reset() {
        selectedList.clear()
        adapter?.setSelectedList(mutableSetOf())
    }

    override fun onSelected(selectPosSet: MutableSet<Int>?) {

    }
}